<template>
  <div id="homebarcharts" :class="className" :style="{ height: height, width: width }" />
</template>

<script setup lang="ts">
import useResize from '@/components/charts/mixins/resize';
import { init, type EChartsOption } from 'echarts'
import { nextTick } from 'process';
import { onActivated, onBeforeUnmount, onDeactivated, onMounted } from 'vue';

const animationDuration = 6000

const {
  mounted,
  chart,
  beforeDestroy,
  activated,
  deactivated
} = useResize()

const props = withDefaults(defineProps<{
  className?: string,
  width?: string,
  height?: string
}>(), {
  className: () => 'chart',
  width: () => '100%',
  height: () => '350px'
})

const initChart = () => {
  const barChart = init(document.getElementById('homebarcharts') as HTMLDivElement, 'macarons')
  barChart.setOption({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      top: 10,
      left: '2%',
      right: '2%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value',
      axisTick: {
        show: false
      }
    }],
    series: [{
      color: ['#57CAEB', '#5DDAB4', '#57CAEB', '#435EBE'],
      name: 'pageA',
      type: 'bar',
      stack: 'vistors',
      // barWidth: '60%',
      data: [79, 52, 200, 334, 390, 330, 220],
      animationDuration
    }, {
      name: 'pageB',
      type: 'bar',
      stack: 'vistors',
      // barWidth: '60%',
      data: [80, 52, 200, 334, 390, 330, 220],
      animationDuration
    }, {
      name: 'pageC',
      type: 'bar',
      stack: 'vistors',
      // barWidth: '60%',
      data: [30, 52, 200, 334, 390, 330, 220],
      animationDuration
    }]
  } as EChartsOption)
  chart.value = barChart
}

onMounted(() => {
  mounted()
  nextTick(() => {
    initChart()
  })
})

onBeforeUnmount(() => {
  beforeDestroy()
})

onActivated(() => {
  activated()
})

onDeactivated(() => {
  deactivated()
})
</script>

<style scoped></style>
